package com.fjnldx.mes.service.dataDictionary;

import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.ResultObject;
import com.fjnldx.mes.domain.pojo.FzyDictionaryData;
import com.fjnldx.mes.domain.pojo.FzyDictionaryDataList;
import com.fjnldx.mes.mapper.dataDictionary.DataDictionaryMapper;
import com.fjnldx.mes.utils.Common;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DataDictionaryService {

    @Resource
    private Common common;
    @Resource
    private DataDictionaryMapper dataDictionaryMapper;

    /**
     * 查询数据字典
     */
    public Result queryDataDictionary(FzyDictionaryData fzyDictionaryData) throws Exception {
        try {
            List<FzyDictionaryData> list = dataDictionaryMapper.queryDataDictionary(fzyDictionaryData);
            int total = dataDictionaryMapper.queryDataDictionaryTotal(fzyDictionaryData);
            ResultObject<FzyDictionaryData> res = new ResultObject<>();
            res.setDataList(list);
            res.setTotal(total);
            return Result.success(res);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 查询数据字典列表
     */
    public Result queryDataDictionaryList(FzyDictionaryDataList fzyDictionaryDataList) throws Exception {
        try {
            List<FzyDictionaryDataList> list = dataDictionaryMapper.queryDataDictionaryList(fzyDictionaryDataList);
            int total = dataDictionaryMapper.queryDataDictionaryListTotal(fzyDictionaryDataList);
            ResultObject<FzyDictionaryDataList> res = new ResultObject<>();
            res.setDataList(list);
            res.setTotal(total);
            return Result.success(res);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 新增数据字典
     */
    public Result insertDataDic(FzyDictionaryData data) throws Exception {
        try {
            List<FzyDictionaryData> queryRes = dataDictionaryMapper.queryDataDictionary(new FzyDictionaryData(data.getBh(),-1));
            if (queryRes.size()>0){
                return Result.error("字典编号重复");
            }
            dataDictionaryMapper.insertDataDic(data);
            return Result.success("新增成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }
    /**
     * 新增数据字典成员列表
     */
    public Result insertDataDicList(FzyDictionaryDataList data) throws Exception {
        try {
            List<FzyDictionaryDataList> queryRes = dataDictionaryMapper.queryDataDictionaryList(new FzyDictionaryDataList(data.getDic_bh(), data.getKey_name(), -1));
            if (queryRes.size()>0){
                return Result.error("键重复");
            }
            dataDictionaryMapper.insertDataDicList(data);
            return Result.success("新增成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 删除数据字典
     */
    public Result deleteDataDic(String[] array) throws Exception {
        try {
            dataDictionaryMapper.deleteDataDic(array);
            return Result.success("删除成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 删除数据字典成员列表
     */
    public Result deleteDataDicList(String[] array) throws Exception {
        try {
            dataDictionaryMapper.deleteDataDicList(array);
            return Result.success("删除成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 更新数据字典
     */
    public Result updateDataDic(FzyDictionaryData data) throws Exception {
        try {
            dataDictionaryMapper.updateDataDic(data);
            return Result.success("更新成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 更新数据字典成员列表
     */
    public Result updateDataDicList(FzyDictionaryDataList data) throws Exception {
        try {
            dataDictionaryMapper.updateDataDicList(data);
            return Result.success("更新成功");
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
